package y2016.date0510.pm.数组闭环替换;

import org.junit.jupiter.api.Test;

public class ResplaceNum {
    private static final int ARRAY[][] = {
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0},
            {0, 1, 0, 1, 1, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
            {0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
            {0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0},
            {0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
            {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0},
            {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0},
            {0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
            {0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
            {0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0},
            {0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
            {0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
    };
    private static int biNum;


    @Test
    public void main() throws Exception {
        StringBuffer nc = new StringBuffer();
        int[][] nums = ARRAY.clone();//比较复杂的逻辑，防止数据出错不能挽回

        System.out.println("矩阵初始：\n");
        //初始矩阵打印
        printlnArrays(nums);

        for (int i = 0; i < nums.length; i++) {
            int[] hang = nums[i];

            for (int j = 0; j < hang.length; j++) {
                int ge = hang[j];
                boolean topFlag = false;
                boolean buttomFlag = false;
                boolean leftFlag = false;
                boolean rightFlag = false;

                if (ge == 0) {
                    //判断上下方
                    for (int k = 0; k < nums.length; k++) {
                        if (nums[k][j] == 1) {
                            if (k < i) {
                                topFlag = true;
                            } else if (k > i) {
                                buttomFlag = true;
                            }
                        }
                    }
                    //判断前后方
                    for (int l = 0; l < hang.length; l++) {
                        if (nums[i][l] == 1) {
                            if (l < j) {
                                leftFlag = true;
                            } else if (l > j) {
                                rightFlag = true;
                            }
                        }
                    }

                    if (leftFlag && rightFlag && topFlag && buttomFlag) {
                        hang[j] = 2;

                    }

                }

            }

        }

        /*
         * 第一次筛选完毕，输出数组
         */
        System.out.println("第一次筛选结果：\n");
        printlnArrays(nums);

        /*
         * 开始第二次筛选
         */

        boolean beihighFlag = true;
        boolean frontFlag = true;
        for (int j = 0; j < nums.length; j++) {
            int[] hang = nums[j];
            for (int k = 0; k < hang.length; k++) {
                int ge = hang[k];

                if (ge == 2) {
                    //遍历该行，右边离1最近部分存在0，说明没有闭合
                    for (int l = k; l < hang.length; l++) {
                        int ls = hang[l];
                        if (ls == 0) {
                            beihighFlag = false;
                            break;
                        }
                        if (ls == 1) {
                            break;
                        }
                    }
                    //遍历该行，左边离1最近部分存在0，也说明没有闭合
                    for (int l = k; l > 0; l--) {
                        int ls = hang[l];
                        if (ls == 0) {
                            frontFlag = false;
                            break;
                        }
                        if (ls == 1) {
                            break;
                        }

                    }

                }

            }
        }

        System.out.println("筛选结果：\n");
        if (frontFlag == false || beihighFlag == false) {
            System.out.println("可能至少存在一个环未闭合");
            //此处应该遍历数组，将所有改成2的值变回来――0，因使用了数组克隆，所以偷懒了
            return;
        }
        printlnArrays(nums);
    }


    private static void printlnArrays(int[][] nums) {
        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < nums.length; i++) {
            int[] hang = nums[i];
            for (int j = 0; j < hang.length; j++) {
                int ge = hang[j];
                if (ge == 2) {//改动输出，将2变成，以便更醒目

                    sb.append("② ");
                } else {
                    sb.append(ge + "　");
                }
            }
            sb.append("\n");
        }
        System.out.println(sb);

    }
}


